package android.bignerdranch.xiangmu;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cursoradapter.widget.SimpleCursorAdapter;

public class Register extends AppCompatActivity {
    private static final String PREFS_NAME ="android.bignerdranch.xiangmu" ;

    private SimpleCursorAdapter adapter;
    private Cursor cursor;
    private MyHelper myHelper;
    EditText et_username;
    EditText et_password;
    EditText et_password2;
    Button btn_register;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registered);
        myHelper = new MyHelper(this);
        SQLiteDatabase db = myHelper.getWritableDatabase();
        Init();
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_password.getText().toString().equals(et_password2.getText().toString())) {

                    ContentValues values = new ContentValues();
                    editor.putString("user_name", et_username.getText().toString());
                    editor.putString("password", et_password.getText().toString());
                    values.put("name", et_username.getText().toString().trim());
                    values.put("password", et_password.getText().toString());
                    long q = db.insert("user",null, values);
                    editor.apply();
                    Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
                else
                {
                    Toast.makeText(Register.this, "两次输入密码不统一请检查重新输入", Toast.LENGTH_SHORT).show();
                    et_password.setText("");
                    et_password2.setText("");
                }
            }
        });
    }
    void Init(){
        et_username = findViewById(R.id.username_re);
        et_password = findViewById(R.id.password_re);
        et_password2 = findViewById(R.id.password2_re);
        btn_register = findViewById(R.id.btn_Registered_re);
        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }
}
